package org.example.java_gobang.config;

import org.example.java_gobang.api.GameAPI;
import org.example.java_gobang.api.MatchAPI;
import org.example.java_gobang.api.TestAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket

public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestAPI testAPI;


    @Autowired
    private MatchAPI matchAPI;

    @Autowired
    private GameAPI gameAPI;


    // 通过 .addInterceptors(new HttpSessionHandshakeInterceptor() 这个操作来
    //把 HttpSession ⾥的属性放到 WebSocket 的 session 中
    // 然后就可以在 WebSocket 代码中 WebSocketSession ⾥拿到 HttpSession 中的attribute.
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(testAPI,"/test");
        registry.addHandler(matchAPI,"/findMatch")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
        registry.addHandler(gameAPI,"/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor());;

    }


}
